from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
from utils.common_model import BaseModel
from user.models import User


class School(BaseModel):
    '''
    学校表
    '''
    name = models.CharField(max_length=64)
    location = models.CharField(max_length=256, blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    # 外键
    service = models.ManyToManyField(to='server_provider.Service', db_table='ccsp_school_service')
    banner = models.ManyToManyField(to='Banner', db_table='ccsp_school_banners', related_name='banners')

    class Meta:
        db_table = 'ccsp_school'
        verbose_name_plural = '学校表'


class Position(BaseModel, MPTTModel):
    '''
    学校岗位表
    '''
    name = models.CharField(max_length=64)
    description = models.TextField(blank=True, null=True)
    is_top = models.BooleanField(default=False, verbose_name='是否是顶级部门')
    # 外键
    school = models.ForeignKey(School, on_delete=models.CASCADE)
    # 自关联部门  # 使用mptt实现树形结构
    parent = TreeForeignKey(to='self', on_delete=models.CASCADE, related_name='menus',
                            verbose_name='自关联部门表', help_text='部门负责数据控制', null=True)

    class MPTTMeta:
        order_insertion_by = ['name']

    class Meta:
        db_table = 'ccsp_school_position'
        verbose_name_plural = '学校岗位表'


class Role(BaseModel):
    '''
    角色表
    '''
    name = models.CharField(max_length=64, unique=True)
    description = models.TextField(blank=True, null=True)

    # 外键：部门-负责控制数据
    position = models.ForeignKey(to=Position, verbose_name='角色-部门表', null=True,
                                 help_text='存放角色与部门的对应关系，负责数据控制', on_delete=models.SET_NULL)
    menu = models.ManyToManyField(to='backend.Menu', db_table='ccsp_role_menu', verbose_name='角色-菜单表',
                                  help_text='存放角色与菜单的对应关系，负责后台菜单的显示')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'ccsp_role'
        verbose_name_plural = '角色表'


class Banner(models.Model):
    title = models.CharField(max_length=64)
    type_choice = (
        (1, '直接跳'),
        (2, '关闭当前页面跳'),
        (3, '跳转到tabbar')
    )
    type = models.IntegerField(choices=type_choice)
    path = models.TextField()
    img = models.FileField(upload_to='banner', default='banner/default.png')

    class Meta:
        db_table = 'ccsp_school_banner'
        verbose_name_plural = '学校轮播图表'

    def __str__(self):
        return 'banner_' + self.title


class AddrPoint(models.Model):
    name = models.CharField(max_length=32)
    addr = models.CharField(max_length=256)
    type_choice = {
        (0, '快递点'),
        (1, '楼栋')
    }
    type = models.IntegerField(choices=type_choice)
    school = models.ForeignKey(to='School', on_delete=models.SET_NULL, null=True)

    class Meta:
        db_table = 'ccsp_addr_point'
        verbose_name_plural = '地点表'
        verbose_name = verbose_name_plural

    def __str__(self):
        return 'addr_point_' + self.name


class Task(BaseModel):
    price = models.FloatField(verbose_name='金额', null=False)
    desc = models.TextField(null=True, blank=True)
    size_choice = (
        (0, '大件'),
        (1, '中件'),
        (2, '小件')
    )
    size = models.IntegerField(choices=size_choice, default=2, help_text='快递代取任务的快递大小', null=True)
    category = models.ForeignKey(to='server_provider.Service', on_delete=models.DO_NOTHING)
    from_addr = models.ForeignKey(to=AddrPoint, on_delete=models.DO_NOTHING, related_name='from_addr',
                                  db_constraint=False)
    to_addr = models.ForeignKey(to='user.Address', on_delete=models.DO_NOTHING, related_name='to_addr',
                                db_constraint=False)
    status_choice = (
        (0, '待付款'),
        (1, '需要帮助'),
        (2, '已帮助'),
        (3, '已完成'),
        (4, '已取消'),
    )
    task_status = models.IntegerField(choices=status_choice, default=0, verbose_name='任务状态')

    consumer = models.ForeignKey(to='user.User', on_delete=models.DO_NOTHING, related_name='consumer', null=True)
    producer = models.ForeignKey(to='user.User', on_delete=models.DO_NOTHING, related_name='producer', null=False)
    school = models.ForeignKey(to='School', on_delete=models.DO_NOTHING)

    class Meta:
        verbose_name_plural = '任务表'
        db_table = 'ccsp_task'

    def __str__(self):
        return 'task_' + self.desc
